module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :push_store

    def connect
      self.push_store = find_verified_user.store
    end

    protected
    def find_verified_user
      session_key = Rails.application.config.session_options[:key]
      user_id = cookies.encrypted[session_key]['user_id']

      if verified_user = StoreStaff.find_by(id: user_id)
        verified_user
      else
        reject_unauthorized_connection
      end
    end

  end
end
